<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\DailyBeanOrderInfo;
use App\Admin\Repositories\DailyBeanRelease;
use App\Models\OrderBeanRewards;
use App\Models\UserBeanPackages;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class DailyBeanReleaseController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new DailyBeanRelease(['user']), function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableRefreshButton();
            $grid->disableActions();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('source_type', '来源', [
                    UserBeanPackages::class => 'KC通证礼包',
                    OrderBeanRewards::class => '拼团奖励',
                ]);
            });
            $grid->column('id')->sortable();
            $grid->column('source_type', '释放来源')->using([
                UserBeanPackages::class => 'KC通证礼包',
                OrderBeanRewards::class => '拼团奖励',
            ]);

            $grid->column('user.name', '用户名');
            $grid->column('user.flag', '用户标签');
            $grid->column('user.telephone', '会员电话')->copyable();

//            $grid->column('source_id');
            $grid->column('release_date');
            $grid->column('beans_released');
            $grid->column('remaining_beans');
            $grid->column('source', '来源订单')
                ->display(function ($source) {
                    if ($this->source_type == OrderBeanRewards::class) {
                        return '订单信息';
                    }
                    return '礼包信息';
                })
                ->expand(function () {
                    return (new DailyBeanOrderInfo())->payload(['id' => $this->source_id, 'type' => $this->source_type]);
                });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('user.name', '用户名');
                $filter->equal('user.flag', '用户标签');
                $filter->equal('user.telephone', '用户电话');
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new DailyBeanRelease(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('source_type');
            $show->field('source_id');
            $show->field('release_date');
            $show->field('beans_released');
            $show->field('remaining_beans');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DailyBeanRelease(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('source_type');
            $form->text('source_id');
            $form->text('release_date');
            $form->text('beans_released');
            $form->text('remaining_beans');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
